import time
import requests
import json
import pandas as pd

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0'
}


# 定义主函数
def main():
    # 初始化一个列表
    info_list = []
    # 循环爬取45页
    for i in range(1, 46):
        # 设置参数
        params = {
            'keyword': '北京',
            'page': i
        }
        # 打印爬取页码
        print('正在爬取第{}页'.format(i))
        # 发送请求
        html = req(params)
        # 解析页面
        info_list += parse(html)
        # 等待2秒
        time.sleep(2)
    # 保存到csv文件
    save_to_csv(info_list)


# 定义一个函数req，用于发送请求
def req(params):
    # 设置请求的url
    url = 'https://piao.qunar.com/ticket/list.json'
    # 发送请求，并设置请求头
    response = requests.get(url, headers=headers, params=params)
    # 返回响应文本
    return response.text


# 定义一个函数parse，用于解析html文件
def parse(html):
    # 使用json模块加载html文件
    data = json.loads(html)
    # 从data中获取sightList
    sight_list = data.get('data', {}).get('sightList', [])
    # 创建一个空列表info_list
    info_list = []
    # 遍历sight_list
    for sight in sight_list:
        # 从sight中获取sightId
        sight_id = sight.get('sightId', '')
        # 从sight中获取sightName
        name = sight.get('sightName', '')
        # 从sight中获取score
        score = sight.get('score', '无')
        # 从sight中获取star
        star = sight.get('star', '无')
        # 从sight中获取saleCount
        sale_count = sight.get('saleCount', '0')
        # 从sight中获取intro
        intro = sight.get('intro', '无')
        # 从sight中获取address
        address = sight.get('address', '无')
        # 从sight中获取qunarPrice
        price = sight.get('qunarPrice', '无')
        # 从sight中获取discount
        discount = sight.get('discount', '无')
        # 从sight中获取point
        point = sight.get('point', '无')
        # 将获取到的信息添加到info_list中
        info_list.append([sight_id, name, score, star, sale_count, intro, address, price, discount, point])
    # 打印info_list
    print(info_list)
    # 返回info_list
    return info_list


# 保存数据到csv文件
def save_to_csv(info_list):
    # 将info_list中的数据转换为DataFrame
    df = pd.DataFrame(info_list,columns=['id', 'name', 'score', 'star', 'sale_count', 'intro', 'address', 'price', 'discount','point'])
    # 将DataFrame保存到csv文件中
    df.to_csv('../static/data/sight.csv', index=False, encoding='utf-8')


if __name__ == '__main__':
    main()